package com.zhou.project.modules.circle.circlePostsYes.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.project.configurations.security.authentication.CurrentAuthentication;
import com.zhou.project.modules.circle.circleGroupPosts.entity.CircleGroupPosts;
import com.zhou.project.modules.circle.circlePostsYes.entity.PostYes;
import com.zhou.project.modules.circle.circlePostsYes.service.PostsYesService;
import com.zhou.project.modules.system.user.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("circle/yes")
@Api(tags = "[帖子点赞]")
public class PostsYesController {

    @Autowired
    private PostsYesService postsYesService;
    @Autowired
    private CurrentAuthentication currentAuthentication;

    @PostMapping
    @ApiOperation("点赞创建和取消 一次点赞，两次取消点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name="postsId",value = "要点赞的帖子的id",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId",value = "点赞人的id",required = true,dataTypeClass = Integer.class)
    })
    @PreAuthorize("hasAuthority('yes:save')")
    public void save(@RequestBody @Valid PostYes entity)
    {
        entity.setUserId(currentAuthentication.getCurrentUserId());
        if (postsYesService.oneYes(entity.getPostsId())) {
            postsYesService.save(entity);
        }else {
            postsYesService.deleteTrue(entity.getPostsId());
        }
    }

    @PostMapping("count/{postsId}")
    @ApiOperation("点赞数量查询")
    @ApiImplicitParam(name = "postsId",value = "要查询的点赞数量",required = true,dataTypeClass = Integer.class)
    public long YesCount(@PathVariable @NotNull(message = "话题id不允许为空") int postsId)
    {
        return postsYesService.queryYes(postsId);
    }

    @PostMapping("user/{size}")
    @ApiOperation("查询话题被谁点赞")
    @ApiImplicitParam(name = "size",value = "查询的最大数量",required = false,dataTypeClass = Integer.class)
    @PreAuthorize("hasAuthority('yes:user')")
    public List<SysUser> queryUser(@PathVariable int size)
    {
        PostYes entity = new PostYes();
        entity.setSize(size);
        return postsYesService.queryUser(entity);
    }

    @PostMapping("posts")
    @ApiOperation("查询用户点赞的话题")
    @ApiImplicitParam(name = "size",value = "查询的最大数量",required = false,dataTypeClass = Integer.class)
    @PreAuthorize("hasAuthority('yes:posts')")
    public List<CircleGroupPosts> queryPosts(@RequestBody PostYes entity)
    {
        return postsYesService.queryPosts(entity);
    }


}
